from dao.job_dao import JobDao
from model.job import Job
import math


class JobService:
    def __init__(self, session):
        self.session = session

    def add(self, job):
        dao = JobDao(self.session)
        dao.add(job)

    def get(self, job_id):
        dao = JobDao(self.session)
        job = dao.get_by_id(job_id=job_id)
        return job

    def merge(self, job):
        dao = JobDao(self.session)
        dao.merge(job)

    def get_job_detail(self, job_id):
        sql = """
        select  a.job_id,a.job_name,a.job_status,a.salary_code,a.part_salary,a.job_time,
		a.job_type,a.job_class,a.job_class_name,a.job_post_name,a.job_post,
		a.job_place,a.job_count,a.job_content,a.job_requirement,a.balance_type,
		a.job_remark,a.company_id,a.job_cycle,province.`name` as province_name,
        city.`name` as city_name,
        country.`name` as country_name,
        a.province_code,a.city_code,a.country_code,
        a.edu_code,a.exp_req,
        a.welfare,a.age_req,a.sex_req,a.attach_req
        from  job a
        left join
        province_city as province
        on a.province_code=province.code
        left join province_city as city
        on a.city_code=city.code
        left join province_city as country
        on a.country_code=country.code
        where a.job_id=:job_id
        """
        result = self.session.execute(sql, {"job_id": job_id})
        if result:
            for row in result:
                return {
                    'job_id': row.job_id,
                    'job_name': row.job_name,
                    'salary_code': row.salary_code if row.salary_code else '',
                    'part_salary': row.part_salary if row.part_salary else '',
                    'job_status': row.job_status,
                    'job_time': row.job_time,
                    'job_type': row.job_type,
                    'job_class': row.job_class,
                    'job_place': row.job_place,
                    'job_count': row.job_count,
                    'job_content': row.job_content if row.job_content else '',
                    'job_requirement': row.job_requirement if row.job_requirement else '',
                    'job_remark': row.job_remark if row.job_remark else '',
                    'job_cycle': row.job_cycle if row.job_cycle else '',
                    'company_id': row.company_id,
                    'province_code': row.province_code,
                    'city_code': row.city_code,
                    'country_code': row.country_code,
                    'balance_type': row.balance_type,
                    'province_name': row.province_name,
                    'city_name': row.city_name,
                    'country_name': row.country_name,
                    'edu_code': row.edu_code,
                    'exp_req': row.exp_req,
                    'welfare': row.welfare if row.welfare else '',
                    'age_req': row.age_req  if row.age_req else '',
                    'sex_req': row.sex_req  if row.sex_req else '',
                    'attach_req': row.attach_req  if row.attach_req else '',
                }
        else:
            return None

    def get_uuid(self, cuuid):
        return self.session.query(Job).filter(Job.j_uuid == cuuid).first()
        if job:
            return job.json()
        else:
            return None

    """
        {
        "page":"1",
        "job_type":"1",
        "province_id":"",
        "city_id":"",
        "key":""
        }
        """

    def query_job(self, province_code=None,
                  city_code=None,
                  key=None,
                  job_type=3,
                  job_class=None,
                  job_post=None,
                  country_code=None,
                  page=0,
                  limit=0,user_id=None):
        dao = JobDao(self.session)
        result = dao.get_list(province_code=province_code,
                              city_code=city_code,
                              key=key,
                              job_type=job_type,
                              job_class=job_class,
                              job_post=job_post,
                              country_code=country_code,
                              page=page, limit=limit,user_id=user_id)
        if result:
            return result
        else:
            return None

    # 根据公司id获得发表的职位信息
    def get_job_by_company_id(self, company_id, **kwargs):
        q = self.session.query(Job).filter(Job.company_id == company_id)
        total = q.count()
        page_size = kwargs.get('page_size', 10)
        page = kwargs.get('page', 1)
        res = q.offset((page - 1) * page_size).limit(page_size).all()
        total_number = q.count()
        records = []
        for data in res:
            records.append(data.json())

        return {'list': records,
                'total': total,
                "cur_page": page,
                "total_page": math.ceil(total_number / page_size)}


if __name__ == '__main__':
    # from utils.work_db import Session
    # ses = Session()
    # jj = JobService(ses)
    # job = jj.get(6)
    # print(job["company_id"])
    pass
